package server;

import java.io.*;
import java.net.*;
import java.util.Scanner;
/**********************************************************************
*
* Filename: Draft_Server.java
*
* File Description:
*  Main Class for running the server and handling clients requests
*
* Author:
* @author Tim Schmalz
* Date Created:
*  Oct 9, 2009
*
**********************************************************************/
public class Draft_Server
{
        public static boolean isRunning = false;
        private static final int PORT = 1129;
        private static Server_TCP_Connection serverConnection;
        private static Socket clientSocket = null;
        /**
	 * @param args the command line arguments
	 */
	public static void main(String[] args)
	{
                Scanner in = new Scanner(System.in);
                String input = "";

                System.out.println("Server started...");
                System.out.println("Ready for admin input...");

                //Take user input
                while(!input.equalsIgnoreCase("quit"))
                {
                    input = in.next();

                    if(input.equalsIgnoreCase("quit"))
                    {
                        System.out.println("Closing Server...");
                        if(isRunning == true)
                        {
                            try
                            {
                                serverConnection.closeSocket();
                            }
                                catch (IOException ex)
                            {
                                System.err.println("An error occurred while closing the socket");
                            }
                        }
                        isRunning = false;
                    }
                    else if(input.equalsIgnoreCase("run"))
                    {
                        System.out.println("Attempting to open port...");
                        isRunning = true;
                        new SocketThread().start();
                        System.out.println("Now listening for clients...");
                    }
                    else if(input.equalsIgnoreCase("help"))
                    {
                        System.out.println("quit - Quit Program");
                        System.out.println("run  - Begin listening for clients");
                        System.out.println("help - Displays commands");
                    }
                    else
                    {
                        System.out.println("Invaild input");
                    }
                }
               //Everything closed correctly
                System.out.println("Good Bye!");
                System.exit(0);
	}

        /**
         * Private thread to listen for new users
         */
        private static class SocketThread extends Thread
        {
            /**
             * Execute Thread to listen to socket
             */
            public void run()
            {
                try
                {
                    serverConnection = new Server_TCP_Connection(PORT);
                    System.out.println("Port: " + PORT + " is now open...");
                    while(isRunning)
                    {
                        //Waits for the next client to connect
                        clientSocket = serverConnection.waitForClients();
                        //Creates a new thread for the accepted client
                        if(clientSocket != null)
                        {
                            Client_Handler client = new Client_Handler(clientSocket);
                            client.start();
                            System.out.println("Client Thread Spawned...");
                        }                            
                    }
                }
                catch(IOException ioe)
                {
                    System.err.println("Failed to accept new clients");
                    isRunning = false;
                }
            }
        }
}
